import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        print("2[a]3[bc]");
        System.out.println("=====");
        print("3[2[5[bc]]]");
    }
    static void print(String content) {
        Stack<Character> stack = new Stack<>();
        int length = content.length();
        for (int i = 0; i < length; i++) {
            char c = content.charAt(i);
            if (Character.isDigit(c)) {
                stack.push(c);
            } else if (c == '[') {
            } else {
                String s = "";
                while (c != ']') {
                    s += c;
                    c = content.charAt(++i);
                }
                int count = 1;
                while (!stack.isEmpty()) {
                    count = count * (stack.pop() - '0');
                }
                if (s.length() > 0) {
                    printStr(count, s);
                }
            }
        }
    }

    private static void printStr(int count, String s) {
        for (int i = 0; i < count; i++) {
            System.out.print(s);
        }
    }
}
